package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo){
        PageInfo<User> pageInfo = userService.findAllUserByPage(userVo);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", pageInfo);
        return responseResult;
    }

    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id, String status){
        if("ENABLE".equalsIgnoreCase(status)){
            status = "DISABLE";
        } else {
            status = "ENABLE";
        }
        userService.updateUserStatus(id, status);
        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功", status);
        return responseResult;
    }

    @RequestMapping("/login")
    public ResponseResult login(String phone, String password, HttpServletRequest request){
        try {
            User user = userService.login(phone, password);
            if (null == user){
                ResponseResult responseResult = new ResponseResult(true, 1, "用户名或密码错误", null);
                return responseResult;
            } else {
                Map<String, Object> map = new HashMap<>();
                String access_token = UUID.randomUUID().toString();
                map.put("access_token", access_token);
                map.put("user_id", user.getId());
                HttpSession httpSession = request.getSession();
                httpSession.setAttribute("user_id", user.getId());
                httpSession.setAttribute("access_token", access_token);
                ResponseResult responseResult = new ResponseResult(true, 1, "响应成功", map);
                return responseResult;
            }
        } catch (Exception e) {
            e.printStackTrace();
            ResponseResult responseResult = new ResponseResult(true, 1, "用户名或密码错误", null);
            return responseResult;
        }
    }

    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(int id){
        List<Role> list = userService.findUserRoleById(id);
        ResponseResult responseResult = new ResponseResult(true, 200, "分配角色成功", list);
        return responseResult;
    }

    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserRoleVo userRoleVo){
        userService.userContextRole(userRoleVo);
        ResponseResult responseResult = new ResponseResult(true, 200, "分配角色成功", null);
        return responseResult;
    }

    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(Integer id){
        ResponseResult responseResult = userService.getUserPermissions(id);
        return responseResult;
    }
}
